
import { AgentType } from "../../lib/util/friendData";

const { ccclass, property } = cc._decorator;

@ccclass
export default class HallMenu extends cc.Component {
	@property(cc.Prefab)
	hallMenuItemPrefab: cc.Prefab = null;

	@property(cc.Node)
	itemList: cc.Node = null;

	itemListSrc = [];

	eventChooseMenuItem = null;
	private becomeAgentEventId: number = null;
	onLoad() {
		var _menuList = cc.vv.GameChooseConfig.menus;
		var _len = _menuList.length;

		for (var i = 0; i < _len; i++) {
			var node = cc.instantiate(this.hallMenuItemPrefab);
			node.parent = this.itemList;
			this.itemListSrc.push(node.getComponent(node.name));
			this.itemListSrc[i].init(_menuList[i]);
			node.height = this.itemList.height;
		}

		this.fill();
	}
	onEnable() {
		this.eventChooseMenuItem = cc.vv.eventCustom.on("chooseMenuItem", this.chooseMenuItem.bind(this), this.node);
		this.becomeAgentEventId = cc.vv.eventCustom.on('updateIdentity', this.updateIdentity.bind(this), this.node);
		this.show();
	}

	onDisable() {
		if (typeof this.eventChooseMenuItem != "undefined") {
			cc.vv.eventCustom.off("chooseMenuItem", this.eventChooseMenuItem);
			this.eventChooseMenuItem = undefined;
		}
		if (typeof this.becomeAgentEventId != "undefined") {
			cc.vv.eventCustom.off("updateIdentity", this.becomeAgentEventId);
			this.becomeAgentEventId = undefined;
		}
	}


	show() {
		// let type = cc.vv.session.getType();
		// if (type > 0) {
		// 	this.updateIdentity([1]);
		// }
	}

	hide() {

	}

	getBottomInfo() {
		let _childrens = this.itemList.children;
		var num = 0;
		var width = 0;
		for (var i = 0; i < _childrens.length; i++) {
			if (_childrens[i].active) {
				num++;
				width += _childrens[i].width;
			}
		}
		return { "num": num, "width": width };
	}

	fill() {
		var type = cc.vv.GameChooseConfig.getMenu();
		for (var i = 0; i < this.itemListSrc.length; i++) {
			this.itemListSrc[i].fill(type);
		}
	}


	chooseMenuItem() {
		this.fill();
	}

	private updateIdentity(args) {
		let type: number = null;
		if (args && typeof args[0] == 'number') type = args[0];
		if (type != null) cc.vv.session.setType(type);
		if (type == AgentType.user)
			this.itemListSrc[cc.vv.GameChooseConfig.menu.club].node.active = false;
		else
			this.itemListSrc[cc.vv.GameChooseConfig.menu.club].node.active = true;
	}

}
